/*
 * Created on Sat Sep 16 2017
 * 
 * Copyright (c) 2017 jianglinjie
 */
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.UploadFile = exports.UploadFileRequest = undefined;

var _NetRequest2 = require('./NetRequest');

var _NetRequest3 = _interopRequireDefault(_NetRequest2);

var _HttpMethod = require('../constants/HttpMethod');

var HttpMethod = _interopRequireWildcard(_HttpMethod);

var _ResponseType = require('../constants/ResponseType');

var ResponseType = _interopRequireWildcard(_ResponseType);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

// export type RNFileInfo = {
//   uri: string,
//   fileName: string,
//   type: string,
// };

var UploadFileRequest = exports.UploadFileRequest = function (_NetRequest) {
  _inherits(UploadFileRequest, _NetRequest);

  function UploadFileRequest(url, urlParam, bodyParams, uploadFiles) {
    var responseType = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : ResponseType.JSON_TYPE;

    _classCallCheck(this, UploadFileRequest);

    var formData = new FormData();

    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
      for (var _iterator = Object.keys(bodyParams)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var key = _step.value;

        formData.append(key, bodyParams[key]);
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion && _iterator.return) {
          _iterator.return();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }

    var _iteratorNormalCompletion2 = true;
    var _didIteratorError2 = false;
    var _iteratorError2 = undefined;

    try {
      for (var _iterator2 = uploadFiles[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
        var file = _step2.value;

        if (file.fileInfo) {
          formData.append(file.key, file.fileInfo);
        }
      }
    } catch (err) {
      _didIteratorError2 = true;
      _iteratorError2 = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion2 && _iterator2.return) {
          _iterator2.return();
        }
      } finally {
        if (_didIteratorError2) {
          throw _iteratorError2;
        }
      }
    }

    return _possibleConstructorReturn(this, (UploadFileRequest.__proto__ || Object.getPrototypeOf(UploadFileRequest)).call(this, url, urlParam, formData, {}, HttpMethod.POST, responseType));
  }

  return UploadFileRequest;
}(_NetRequest3.default);

var UploadFile = exports.UploadFile = function UploadFile(key, fileInfo) {
  _classCallCheck(this, UploadFile);

  this.key = key;
  this.fileInfo = fileInfo;
};